<template>
	<el-dialog :title="title" v-model="visible" :width="950" destroy-on-close @closed="$emit('closed')">
		<scTable height="400" ref="table" :params="search" :pageSize="10" :apiObj="apiObj"
				 @selection-change="selectionChange" @row-mouseup="handleRowClick" @row-dblclick="handleDblclick" stripe
				 remoteSort remoteFilter>
			<el-table-column type="selection" width="50"></el-table-column>
			<el-table-column label="序号" type="index" width="100"></el-table-column>
			<el-table-column label="商品ID" prop="product_id" width="100"></el-table-column>
			<el-table-column label="商品名称" prop="product_name" width="150"></el-table-column>
			<el-table-column label="销售价" prop="sale_price" width="150"></el-table-column>
			<el-table-column label="库存" prop="stock_qty" width="150"></el-table-column>
			<el-table-column label="有效期" prop="valid_date_text" width="140"></el-table-column>
			<el-table-column label="服务商名称" prop="provider_name" width="150"></el-table-column>
		</scTable>
		<el-row>
			<el-text class="mx-1">{{ text_desc }}</el-text>
		</el-row>
		<template #footer>
			<el-button @click="visible = false">关 闭</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	props: {
		company_id: {
			type: Number,
			required: true
		}
	},
	data() {
		return {
			title: "选择商品",
			visible: false,
			isSaveing: false,
			text_desc: "",
			search: {status: 1, is_valid: 1},
			apiObj: this.$API['service_provider.service_provider'].productIndex,
			selection: null,
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(data = {}) {
			this.visible = true;
			if (data.status != undefined) {
				this.search.do_status = data.status
			}
			return this
		},
		//关闭
		close() {
			this.visible = false;
			return this
		},

		//表单提交方法
		submit() {
			this.$emit('success', this.selection)
		},

		handleDblclick: function (row) {
			console.log('===', row);
			this.$refs.table.toggleRowSelection(row);
			this.$emit('success', this.selection)
		},
		//选中当前行
		handleRowClick: function (row) {
			console.log(row);
			this.$refs.table.toggleRowSelection(row);
		},

		//表格选择后回调事件
		selectionChange(selection) {
			console.log('++++++', selection)
			this.selection = selection;
			var total_price = 0;
			selection.forEach(function (item) {
				total_price += parseFloat(item.sale_price)
				console.log(item.product_id, '------------');
			});
			this.text_desc = "最多选择20个,已选择" + selection.length + '个商品,总价：' + total_price.toFixed(2);
		},

		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},

		//搜索重置
		resetUpsearch() {
			this.search = {};
			this.$refs.table.reload(this.search)
		},
	}
}
</script>

<style></style>
